package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;

public class GetNewAppAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		try {
			PrintWriter out =  response.getWriter();
			AppBO appbo = new AppBO();
			ArrayList<App> listApp = new ArrayList<App>();
			listApp = appbo.getNewApp();
			
			String applistHTML = "";
			int i=0;
			for(App temp:listApp){
				ArrayList<String> avatar = appbo.getNumberOfFile(getServlet().getServletContext().getRealPath("/")+temp.getLink()+"/Avatar");
				String avatarLink;
				if(avatar.size()==0){
					avatarLink = "references/images/avatar.jpg"; 
				}else{
					avatarLink = temp.getLink()+"/Avatar/"+avatar.get(0);
				}
				String active="";
				if(i!=(listApp.size()-1)){
					active=" app-icon-active app-icon-active-"+(i+1);	
				}
				
				applistHTML+="<div class='app-icon"+active+"'><a href='getAppDetail.jms?appid="+temp.getAppId()+"' class='app-balloon-toggle-link'> "
						+ "<img class='new-app' src='"+avatarLink+"' width='120px' height='120px' />"
						+ "</a>"
						+ "<div class='app-info-balloon'>"
						+ "<div class='app-title'>"+temp.getAppName()+"</div>"
								+ "<div class='balloon-tail'></div>"
								+ "</div>"
								+ "<div class='shadow'>"
								+ "<div class='shadow-left'></div>"
								+ "<div class='shadow-body'></div>"
								+ "<div class='shadow-right'></div></div></div>";
				i++;
			}
			out.print(applistHTML);
			out.close();
		} catch (IOException | DAOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
}
